package com.huan.web;

import com.alibaba.fastjson.JSON;
import com.huan.pojo.PageBean;

import com.huan.pojo.Teach;
import com.huan.service.TeachService;
import com.huan.service.impl.TeachServiceImpl;
import com.huan.web.base.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;

@WebServlet("/teach/*")
public class TeachServlet extends BaseServlet {
    TeachService service = new TeachServiceImpl();
    /**
     * 分页条件查询
     * @param request
     * @param response
     * @throws Exception
     */
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        //转为对象
        Teach teach = JSON.parseObject(params, Teach.class);


        PageBean<Teach> pageBean = service.selectByPageAndCondition(currentPage, pageSize, teach);

        String s = JSON.toJSONString(pageBean);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(s);
    }

    /**
     * 增加
     * @param request
     * @param response
     * @throws Exception
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println(s);
        Teach teach = JSON.parseObject(s, Teach.class);
        System.out.println(teach);



        boolean flag = service.add(teach);
        if (flag){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("lose");
        }

    }

    /**
     * 编辑数据时传过来的id（有无都可）
     * @param request
     * @param response
     * @throws Exception
     */
    public void acceptId(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        System.out.println("浏览器传过来的："+params);

/*        String acceptId = JSON.parseObject(params, String.class);
        System.out.println("存进session的"+acceptId);*/

        HttpSession session = request.getSession();
        session.setAttribute("acceptId",params);

        response.getWriter().write("success");

    }

    /***
     * 编辑
     * @param request
     * @param response
     * @throws Exception
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();

      /*  HttpSession session = request.getSession();
        String acceptId = (String) session.getAttribute("acceptId");
        System.out.println(acceptId);*/

        Teach teach = JSON.parseObject(params, Teach.class);


        /* student.setStudentID(acceptId);*/
        System.out.println(teach);

        int i = service.update(teach);
        if (i>0){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("lose");
        }
    }

    /**
     * 删除
     * @param request
     * @param response
     * @throws Exception
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String teacherID = request.getParameter("teacherID");
        String courseID = request.getParameter("courseID");

        System.out.println(teacherID+"+"+courseID);

        service.delete(teacherID,courseID);

        response.getWriter().write("success");

    }

}
